#!/bin/sh

# This file is part of telegram-purple
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02111-1301  USA
#
# Copyright Matthias Jentsch, Ben Wiederhake 2016

set -e

# -- Cleanup possibly left-over artifacts
rm -f bin/tgl.tar bin/commit.h.tar bin/result.tar bin/result.tar.gz
# Very old artifact
rm -f bin/tgl_tl-parser.tar
# Setup
mkdir -p bin

# -- Create parts
# Abuse the "bin" dir for temporary files.
( cd tgl && git archive --prefix=telegram-purple/tgl/ --output=../bin/tgl.tar HEAD )
git archive --prefix=telegram-purple/ --output=bin/result.tar HEAD
# This is a lot of options. Here's why.
# --sort --mtime --owner --group => be reproducible (same commit produces binarily identical files)
# --transform                    => be in the same "tar-folder" as the rest
# (absence of --numeric-ids)     => have identical behavior as git-archive
# --mode=664                     => have identical behavior as git-archive
#                                   http://www.gelato.unsw.edu.au/archives/git/0701/36326.html
#                                   This is hacky. TODO: Find a better way to unify permissions.
tar --sort=name --mtime="1970-01-01 00:00Z" --owner=root --group=root --transform s%^%telegram-purple/% --mode=664 -cf bin/commit.h.tar commit.h

# -- Concatenate it all
tar --concatenate -f bin/result.tar bin/tgl.tar
tar --concatenate -f bin/result.tar bin/commit.h.tar
gzip -n bin/result.tar

# -- Determine name and move
TARNAME="telegram-purple_`git describe --tags | sed s/^v// `.orig.tar.gz"
# "mv -f" means "overwrite, if necessary"
echo mv -f bin/result.tar.gz $TARNAME
mv -f bin/result.tar.gz $TARNAME

# -- Cleanup (never fail)
rm -f bin/tgl.tar bin/commit.h.tar bin/result.tar bin/result.tar.gz || true
